// Copyright 2009 Pigasus.
// GNU GPL v2.
// Author: John Zhang.

#include "MiniCppUnit-2.5/MiniCppUnit.hxx"

#include <string>

#include "../src/pigasus-types.h"
#include "../src/formatstring.h"

class CastTest : public TestFixture<CastTest> {
 public:
  TEST_FIXTURE(CastTest) {
    TEST_CASE(TestFloatPrinting);
    TEST_CASE(TestIntPrinting);
    TEST_CASE(TestStringPrinting);
    TEST_CASE(TestMixedPrinting);
  }

  void TestFloatPrinting() {
    List args;
    args.Append(0.25);

    string result = FormatString("%lf", args);
    ASSERT_EQUALS(result, "0.250000");

    args.Append(-0.00001);
    args.Append(10.0);

    result = FormatString("%lf %0.2lf %.0lf", args);
    ASSERT_EQUALS(result, "0.250000 -0.00 10");

    result = FormatString("hello %0.2lf world! %0.10lf%%%0.2lf\n", args);
    ASSERT_EQUALS(result, "hello 0.25 world! -0.0000100000%10.00\n");
  }

  void TestIntPrinting() {
    List args;
    args.Append(1);

    string result = FormatString("%d", args);
    ASSERT_EQUALS(result, "1");
    result = FormatString("%5d", args);
    ASSERT_EQUALS(result, "    1");
    result = FormatString("%02d", args);
    ASSERT_EQUALS(result, "01");

    args.Append(100001);
    args.Append(-10);
    args.Append(42);
    result = FormatString("%d %d %03d %d", args);
    ASSERT_EQUALS(result, "1 100001 -10 42");

    result = FormatString("hello%dworld!", args);
    ASSERT_EQUALS(result, "hello1world!");

    result = FormatString("hello%%%02dworld!\n\n%d\t%d\n", args);
    ASSERT_EQUALS(result, "hello%01world!\n\n100001\t-10\n");
  }

  void TestStringPrinting() {
    List args;
    args.Append("bob");

    string result = FormatString("%s", args);
    ASSERT_EQUALS(result, "bob");
    result = FormatString("hello %s!", args);
    ASSERT_EQUALS(result, "hello bob!");

    args.Append("jane");
    args.Append("bill");
    args.Append("judy");
    result = FormatString("%s%%", args);
    ASSERT_EQUALS(result, "bob%");
    result = FormatString("%s%s%s%s", args);
    ASSERT_EQUALS(result, "bobjanebilljudy");
    result = FormatString("%%%s%%%s%%%s%%%s%%", args);
    ASSERT_EQUALS(result, "%bob%jane%bill%judy%");
  }

  void TestMixedPrinting() {
    List args;
    args.Append(42);
    args.Append(-10.5);
    args.Append("jane");
    args.Append("bob");
    args.Append(43.0);
    args.Append(-10.0000001);

    string result = FormatString("%d:%lf\n%s", args);
    ASSERT_EQUALS(result, "42:-10.500000\njane");

    result = FormatString("%d%0.0lf%s%s%%%%%%%0.5lf", args);
    ASSERT_EQUALS(result, "42-10janebob%%%43.00000");
    
    result = FormatString("%5d\n\t%0.0lf%s%s%0.5lf%lf%%", args);
    ASSERT_EQUALS(result, "   42\n\t-10janebob43.00000-10.000000%");
  }
};

REGISTER_FIXTURE(CastTest);
